﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateTest
{
    public struct Book
    {
        public string Title;
        public string Author;
        public decimal Price;
        public bool Paperback;

        public Book(string title, string author, decimal price, bool paperBack = true)
        {
            Title = title;
            Author = author;
            Price = price;
            Paperback = paperBack;
        }
    }

    public delegate void ProcessBookDelegate(Book b);

    public class BookDB
    {
        ArrayList arrayList = new ArrayList();

        public void AddBook(string title, string author, decimal price, bool paperBack)
        {
            arrayList.Add(new Book(title, author, price, paperBack));
        }

        public void ProcessBook(ProcessBookDelegate d)
        {
            foreach (Book b in arrayList)
            {
                d(b);
            }
        }
    }

    public delegate void Dele(int x, double y);

    public delegate void MyDele();

    class SampleClass
    {
        public void InstanceMethod()
        {
            Console.WriteLine("A message from in Instance Method");
        }

        public static void StaticMethod()
        {
            Console.WriteLine("A message from in Static Method");
        }
    }
    class Program
    {
        static void PrintTitle(Book b)
        {
            Console.WriteLine(b.Title);
        }

        static void PrintBook(Book b)
        {
            Console.WriteLine("Title: " + b.Title + " Author: " + b.Author + " Price: " + b.Price);
        }
        void MultiplyNumbers(int n, double d)
        {
            Console.Write(n * d + " ");
        }

       
        static void Main(string[] args)
        {
            BookDB bookDB = new BookDB();

            bookDB.AddBook("The C Programming Language", "Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true);
            bookDB.AddBook("The Unicode Standard 2.0", "The Unicode Consortium", 39.95m, true);
            bookDB.AddBook("The MS-DOS Encyclopedia", "Ray Duncan", 129.95m, false);
            bookDB.AddBook("Dogbert's Clues for the Clueless", "Scott Adams", 12.00m, true);

            bookDB.ProcessBook(PrintTitle);
            Console.WriteLine("-----------------");
            bookDB.ProcessBook(PrintBook);

            Console.ReadKey();

            SampleClass sc = new SampleClass();
            MyDele md = sc.InstanceMethod;
            md();
            Console.WriteLine("------------");
            md += SampleClass.StaticMethod;
            md();
            Console.WriteLine("------------");
            md -= sc.InstanceMethod;
            md();
            Console.WriteLine("------------");

            //md = SampleClass.StaticMethod;
            //md();


            Console.ReadKey();

            Program p = new Program();

            Dele d = p.MultiplyNumbers;

            for(int i=1; i <= 5; i++)
            {
                //p.MultiplyNumbers(i, 2);
                d(i, 2);
            }

            //PerCalculation d = delegate (int x, int j) { return 0; };

            Console.ReadKey();
        }
    }
}
